package com.nageffer.shortlink.admin.controller;

import com.nageffer.shortlink.admin.common.convention.result.Result;
import com.nageffer.shortlink.admin.common.convention.result.Results;
import com.nageffer.shortlink.admin.dto.req.GroupSaveReqDTO;
import com.nageffer.shortlink.admin.dto.req.GroupSortReqDto;
import com.nageffer.shortlink.admin.dto.req.GroupUpdateReqDTO;
import com.nageffer.shortlink.admin.dto.resp.GroupRespDTO;
import com.nageffer.shortlink.admin.service.GroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 短链接分组控制层
 */
@RestController
@RequiredArgsConstructor
public class GroupController {

    private final GroupService groupService;

    /**
     * 新增短链接分组
     */
    @PostMapping("/api/short-link/admin/v1/group")
    public Result<Void> saveGroup(@RequestBody GroupSaveReqDTO groupSaveReqDTO){
        groupService.saveGroup(groupSaveReqDTO.getName());
        return Results.success();
    }

    /**
     * 查询所有短链接分组
     */
    @GetMapping("/api/short-link/admin/v1/group")
    public Result<List<GroupRespDTO>> listGroup(){
        List<GroupRespDTO> groupRespDTOS = groupService.listGroup();
        return Results.success(groupRespDTOS);
    }

    /**
     * 更新短链接分组
     */
    @PutMapping("/api/short-link/admin/v1/group")
    public Result<Void> updateGroup(@RequestBody GroupUpdateReqDTO groupUpdateReqDTO){
        groupService.updateGroup(groupUpdateReqDTO);
        return Results.success();
    }

    /**
     * 删除短链接分组
     */
    @DeleteMapping("/api/short-link/admin/v1/group") // Delete请求可以将参数通过URL携带
    public Result<Void> updateGroup(String gid){
        groupService.deleteGroup(gid);
        return Results.success();
    }

    /**
     * 对分组信息进行排序
     */
    @PostMapping("/api/short-link/admin/v1/group/sort")
    public Result<Void> sortGroup(@RequestBody List<GroupSortReqDto> groupSortReqDtoList){
        groupService.sortGroup(groupSortReqDtoList);
        return Results.success();
    }

}
